home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / alloca.s < prev    next >
Text File  |  1990-11-23  |  916b  |  40 lines

  1. ****
  2. *
  3. *    alloca()    -- allocate memory from the stack (NOT the heap)
  4. *
  5.  
  6. *
  7. * entry points
  8. *
  9. .globl    _alloca
  10.  
  11. *
  12. * external references
  13. *
  14. .globl    __break
  15.  
  16. .text
  17. *
  18. * char *alloca(size)
  19. *   int size;
  20. *
  21. * Allocate <size> bytes from the stack.  This space is automatically
  22. * free'd when the calling function exits.  DO NOT use other memory
  23. * management function, like free() and realloc(), with this block.
  24. *
  25. _alloca:
  26.     clr.l    d0            * set initial return value
  27.     move.l    (sp),a1            * save return address
  28.     clr.l    d1            * get requested block size
  29.     move.w    4(sp),d1
  30.     move.l    sp,a0            * calculate new sp
  31.     sub.l    d1,a0
  32.     cmp.l    __break,a0        * if (new sp < __break)
  33.     blt    ealloca            *   error, potential stack overflow
  34.     move.l    a0,sp            * set new sp
  35.     move.w    d1,(a0)+        * store dummy word
  36.     move.l    a1,(a0)+        * store dummy long
  37.     move.l    a0,d0            * return pointer to memory block
  38. ealloca:
  39.     jmp    (a1)            * "return" to caller
  40.